<html>
<head><meta charset="utf-8"><title>Casts · project-portable-simd · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/index.html">project-portable-simd</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html">Casts</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212140947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212140947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212140947">(Oct 02 2020 at 23:36)</a>:</h4>
<p>Has anyone thought about how casts should be performed?  Lossless conversions should use <code>From</code>/<code>Into</code>, but what about lossy conversions and byte casts?</p>



<a name="212140988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212140988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212140988">(Oct 02 2020 at 23:37)</a>:</h4>
<p>I think a decent number of functions are blocking on casting to and from vectors of <code>u8</code></p>



<a name="212141477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212141477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212141477">(Oct 02 2020 at 23:47)</a>:</h4>
<p>Well, I think that we should "name the cast" that we're doing and put it as methods.</p>
<p>For example: f32xN::truncate_int, f32xN::round_int, etc, whatever the operation does. There's so many variant ways that we should use explicit method names by default. We might <em>also</em> add trait impls that are backed by the methods if a particular method is the "most common" variant.</p>



<a name="212141650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212141650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212141650">(Oct 02 2020 at 23:50)</a>:</h4>
<p>I think that, for converting to <code>u8</code> sequences, people can get the slice of elements and then use whatever slice casting techniques they want (<code>bytemuck</code>, <code>zerocopy</code>, etc etc).</p>
<p>The Safe Transmute Project is doing well right now, and I think that they'll be able to stabilize within the same time frame we're aiming for (before the end of 2021), so having just mild casting for a while and letting the safe transmute group do the heavy lifting later on seems like a good choice.</p>



<a name="212142000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142000">(Oct 02 2020 at 23:56)</a>:</h4>
<p>I think we're going to need (at least internally) some casting to <code>u8</code> for changing things like endianness, but good point</p>



<a name="212142011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142011">(Oct 02 2020 at 23:57)</a>:</h4>
<p>good idea on waiting for something to stabilize</p>



<a name="212142270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142270">(Oct 03 2020 at 00:00)</a>:</h4>
<p>Hmm, can you give an example of the internal u8 cast you're thinking of?</p>



<a name="212142394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142394">(Oct 03 2020 at 00:02)</a>:</h4>
<p>something like <code>u32x4::swap_bytes</code> I'm guessing will cast to <code>u8x16</code> and do a const shuffle</p>



<a name="212142397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142397">(Oct 03 2020 at 00:02)</a>:</h4>
<p>(and cast back)</p>



<a name="212142486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142486">(Oct 03 2020 at 00:04)</a>:</h4>
<p>Ah, for that we can just have the ever so lovely <code>union bits128 { ... }</code> with all the variants we ever need.</p>



<a name="212142494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142494">(Oct 03 2020 at 00:04)</a>:</h4>
<p>and 256, 512, 64, etc</p>



<a name="212142532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142532">(Oct 03 2020 at 00:05)</a>:</h4>
<p>haha, I would probably just transmute at that point I think?</p>



<a name="212142550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142550">(Oct 03 2020 at 00:05)</a>:</h4>
<p>I was just hoping for a safe interface</p>



<a name="212142634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142634">(Oct 03 2020 at 00:06)</a>:</h4>
<p>I like the union better because you can put in a specific list of "known good" types and verify it it one place. If the field is on the union, you can do that change over, no worries.</p>
<p>You could also put safe methods on the type too for that matter.</p>



<a name="212142882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212142882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212142882">(Oct 03 2020 at 00:10)</a>:</h4>
<p>good point</p>



<a name="212183802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212183802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212183802">(Oct 03 2020 at 18:23)</a>:</h4>
<p>Ideally, safe-transmute should solve this problem. It should be possible to transmute safely between any pair of 128-bit types, for instance.</p>



<a name="212187387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212187387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212187387">(Oct 03 2020 at 19:59)</a>:</h4>
<p>Got it, maybe this is not something that specifically needs to be handled for SIMD (as it is in packed_simd)</p>



<a name="212231022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231022">(Oct 04 2020 at 16:57)</a>:</h4>
<p>I do think that it's important to arrange simple ways to transmute between these types. Especially if they end up using different types than the native SIMD.</p>



<a name="212231027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231027">(Oct 04 2020 at 16:57)</a>:</h4>
<p>That said, in an ideal world, portable SIMD would use exactly the same vector types as the native SIMD.</p>



<a name="212231028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231028">(Oct 04 2020 at 16:57)</a>:</h4>
<p>So that without casting at all, you could trivially switch between portable operations and native operations.</p>



<a name="212231088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231088">(Oct 04 2020 at 16:59)</a>:</h4>
<p>Well, the native types are a little easier so we've implemented From/Into for those, so that part should work fine without safe transmute</p>



<a name="212231841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231841">(Oct 04 2020 at 17:19)</a>:</h4>
<p>That's helpful.</p>



<a name="212231887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231887">(Oct 04 2020 at 17:20)</a>:</h4>
<p>But I'm curious, is there any <em>fundamental</em> reason that the native types and the portable types couldn't be the same type, on each platform?</p>



<a name="212231892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231892">(Oct 04 2020 at 17:20)</a>:</h4>
<p>(At least, when a native type exists?)</p>



<a name="212231995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212231995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212231995">(Oct 04 2020 at 17:22)</a>:</h4>
<p>I imagine it would be fine, but I think that std::arch should probably use the std::simd types and not the other way around</p>



<a name="212232005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232005">(Oct 04 2020 at 17:23)</a>:</h4>
<p>Sure, assuming they match I don't see any reason why not. :)</p>



<a name="212232073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232073">(Oct 04 2020 at 17:24)</a>:</h4>
<p>But the native types done really exactly match.  What is an __m128i? Is it u8x16, u16x8, u32x4, u64x2?</p>



<a name="212232188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232188">(Oct 04 2020 at 17:26)</a>:</h4>
<p>"yes" ;)</p>



<a name="212232210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232210">(Oct 04 2020 at 17:27)</a>:</h4>
<p>All of those types should be easily interchangeable, with at most a <code>.into()</code>.</p>



<a name="212232223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232223">(Oct 04 2020 at 17:27)</a>:</h4>
<p>There are plenty of SIMD algorithms that involve assuming those are interchangeable.</p>



<a name="212232225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232225">(Oct 04 2020 at 17:27)</a>:</h4>
<p>Well I agree they should be interchangeable but I'm not sure about with into</p>



<a name="212232230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232230">(Oct 04 2020 at 17:27)</a>:</h4>
<p>Well, if they could be interchangeable <em>without</em> into, that'd be fine too. ;)</p>



<a name="212232238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232238">(Oct 04 2020 at 17:27)</a>:</h4>
<p>They certainly shouldn't require <code>unsafe</code> to transmute between.</p>



<a name="212232276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232276">(Oct 04 2020 at 17:28)</a>:</h4>
<p>I agree, it should be safe</p>



<a name="212232298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232298">(Oct 04 2020 at 17:29)</a>:</h4>
<p>I'm concerned about confusion of what that conversion does. [u8; 8] doesn't impl  From&lt;u64&gt; because I think that would be confusing</p>



<a name="212232365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232365">(Oct 04 2020 at 17:30)</a>:</h4>
<p>I don't think it'd be that confusing. But regardless, for SIMD it <em>is</em> important to be able to move between <code>[u8;16]</code> and the 128-bit SIMD type, or <code>u32;8</code> and the 256-bit SIMD type.</p>



<a name="212232366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232366">(Oct 04 2020 at 17:30)</a>:</h4>
<p>u16x8 can impl From&lt;u8x8&gt; which is probably more in the style of what From/Into are used for</p>



<a name="212232375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232375">(Oct 04 2020 at 17:30)</a>:</h4>
<p>That's a very different conversion (widening).</p>



<a name="212232385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232385">(Oct 04 2020 at 17:31)</a>:</h4>
<p>Yep, which is what From/Into is currently used for</p>



<a name="212232387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232387">(Oct 04 2020 at 17:31)</a>:</h4>
<p>As compared to a zero-cost reinterpretation of a vector register.</p>



<a name="212232392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232392">(Oct 04 2020 at 17:31)</a>:</h4>
<p>Into for widening and TryInto for narrowing, right?</p>



<a name="212232426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232426">(Oct 04 2020 at 17:32)</a>:</h4>
<p>Widening should be <code>.into()</code> and narrowing should be <code>.try_into()</code>, but the reverse shouldn't be a limitation.</p>



<a name="212232442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232442">(Oct 04 2020 at 17:32)</a>:</h4>
<p><code>.into()</code> is used for lots of other things.</p>



<a name="212232448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232448">(Oct 04 2020 at 17:32)</a>:</h4>
<p>It can convert between Box and Vec, or &amp;str and String.</p>



<a name="212232468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232468">(Oct 04 2020 at 17:33)</a>:</h4>
<p>I can't think of any cases where it changes the underlying type though</p>



<a name="212232469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232469">(Oct 04 2020 at 17:33)</a>:</h4>
<p>It seems like a reasonable thing to use for a same-size zero-cost conversion, if we need any indication at all and can't just use the same type.</p>



<a name="212232475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232475">(Oct 04 2020 at 17:33)</a>:</h4>
<p>What do you mean by "changes the underlying type"?</p>



<a name="212232478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232478">(Oct 04 2020 at 17:33)</a>:</h4>
<p>Not to say it isn't safe, I just can't think of any uses of Into in that way</p>



<a name="212232530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232530">(Oct 04 2020 at 17:34)</a>:</h4>
<p>Well even something as simple as From&lt;i8&gt; for u8, I'd have to check but I don't think that exists</p>



<a name="212232537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232537">(Oct 04 2020 at 17:34)</a>:</h4>
<p>That <em>shouldn't</em> exist, but <code>TryFrom</code> should.</p>



<a name="212232552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232552">(Oct 04 2020 at 17:35)</a>:</h4>
<p>Right, but that's my point. It's a narrowing op, not a cast</p>



<a name="212232567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232567">(Oct 04 2020 at 17:35)</a>:</h4>
<p>That one isn't "narrowing", exactly. But I sorta get what you mean.</p>



<a name="212232573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232573">(Oct 04 2020 at 17:35)</a>:</h4>
<p>From&lt;Vec&lt;u16&gt;&gt; for Vec&lt;u8&gt; _could_ exist but I'm not sure that it should?</p>



<a name="212232617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232617">(Oct 04 2020 at 17:36)</a>:</h4>
<p>It seems that From/Into never changes the meaning of the data</p>



<a name="212232620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232620">(Oct 04 2020 at 17:36)</a>:</h4>
<p>Right. I think I understand what you're getting at; you're suggesting that <em>reinterpretation</em> is different.</p>



<a name="212232623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232623">(Oct 04 2020 at 17:36)</a>:</h4>
<p>Yes, that's what I'm getting at</p>



<a name="212232626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232626">(Oct 04 2020 at 17:36)</a>:</h4>
<p>safe-transmute does seem like it more accurately captures the intent.</p>



<a name="212232631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232631">(Oct 04 2020 at 17:36)</a>:</h4>
<p>"change these bytes to a different type that's compatible".</p>



<a name="212232644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232644">(Oct 04 2020 at 17:37)</a>:</h4>
<p>You should be able to safe-transmute <code>Vec&lt;u16&gt;</code> to <code>Vec&lt;u8&gt;</code>, and <code>i8</code> to <code>u8</code> or vice versa.</p>



<a name="212232652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232652">(Oct 04 2020 at 17:37)</a>:</h4>
<p>Which brings us back to, what should it look like to reinterpret between SIMD types.</p>



<a name="212232697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232697">(Oct 04 2020 at 17:38)</a>:</h4>
<p>Yep. Well transmuting Vec depends on the implementation (size in bytes vs elements) but yep</p>



<a name="212232704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232704">(Oct 04 2020 at 17:38)</a>:</h4>
<p>Not sure what you mean by that last bit.</p>



<a name="212232726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232726">(Oct 04 2020 at 17:39)</a>:</h4>
<p>If the capacity/used size in a Vec is represented by number of elements instead of number of bytes you can't simply transmute them</p>



<a name="212232730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232730">(Oct 04 2020 at 17:39)</a>:</h4>
<p>But that's irrelevant to this haha</p>



<a name="212232754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232754">(Oct 04 2020 at 17:39)</a>:</h4>
<p>Ah, sure; I was thinking of the underlying data, not the Vec's fields like capacity and length.</p>



<a name="212232760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232760">(Oct 04 2020 at 17:39)</a>:</h4>
<p>So I should say differently:</p>



<a name="212232811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232811">(Oct 04 2020 at 17:40)</a>:</h4>
<p>It should be possible to safe-transmute between <code>[u16]</code> and <code>[u8]</code>.</p>



<a name="212232820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232820">(Oct 04 2020 at 17:40)</a>:</h4>
<p>Yeah, I agree</p>



<a name="212232828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232828">(Oct 04 2020 at 17:40)</a>:</h4>
<p>And similarly some SIMD types</p>



<a name="212232854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232854">(Oct 04 2020 at 17:41)</a>:</h4>
<p>So, safe-transmute does seem like a reasonable fit there. I think the bigger question is whether you should need an explicit conversion at all.</p>



<a name="212232859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232859">(Oct 04 2020 at 17:41)</a>:</h4>
<p>It could get confusing, like transmuting u8 back to f32 is fine but you're pretty likely to get nans if you're not careful</p>



<a name="212232864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232864">(Oct 04 2020 at 17:41)</a>:</h4>
<p>Yeah</p>



<a name="212232925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232925">(Oct 04 2020 at 17:42)</a>:</h4>
<p>There are absolutely algorithms where, instruction-by-instruction, you want to treat a 256-bit register as 4 64-bit values or 32 8-bit values.</p>



<a name="212232934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232934">(Oct 04 2020 at 17:42)</a>:</h4>
<p>There's also value in type-safety.</p>



<a name="212232955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232955">(Oct 04 2020 at 17:43)</a>:</h4>
<p>I'm wondering if it might make sense to guarantee (type-wise) that they all have the same memory representation, such that they're all transparent newtype wrappers where .0 is the internal value that works in any of them.</p>



<a name="212232972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212232972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212232972">(Oct 04 2020 at 17:43)</a>:</h4>
<p>My general thought is that with From/Into it should "just work" as long as it compiles, so we might want a different trait to indicate it might not "just work" while still being safe</p>



<a name="212233027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233027">(Oct 04 2020 at 17:44)</a>:</h4>
<p>I mean, they're all just bits, and they have the same alignment requirement, so it'll definitely "work" in some sense; the question is if someone may accidentally use an operation they didn't intend.</p>



<a name="212233029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233029">(Oct 04 2020 at 17:44)</a>:</h4>
<p>So I think the reason that doesn't work is the way that platform-intrinsics work</p>



<a name="212233052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233052">(Oct 04 2020 at 17:45)</a>:</h4>
<p>How so?</p>



<a name="212233054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233054">(Oct 04 2020 at 17:45)</a>:</h4>
<p>As far as I can tell it uses the underlying type somehow to figure out which function to use</p>



<a name="212233071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233071">(Oct 04 2020 at 17:45)</a>:</h4>
<p>Which monomorphization if that's the right word</p>



<a name="212233074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233074">(Oct 04 2020 at 17:45)</a>:</h4>
<p>They're generic</p>



<a name="212233122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233122">(Oct 04 2020 at 17:46)</a>:</h4>
<p>Interesting. That seems like a property of the language-level intrinsics, rather than the compiler-level intrinsic operations.</p>



<a name="212233133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233133">(Oct 04 2020 at 17:46)</a>:</h4>
<p>But in any case, the newtypes can call the right intrinsics easily enough.</p>



<a name="212233169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233169">(Oct 04 2020 at 17:47)</a>:</h4>
<p>Without calling the llvm intrinsics directly I'm not so sure</p>



<a name="212233335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233335">(Oct 04 2020 at 17:51)</a>:</h4>
<p>My only thought is maybe the platform-intrinsics panic on overflow in debug mode?  Otherwise I'm not sure what the difference could be</p>



<a name="212233347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233347">(Oct 04 2020 at 17:51)</a>:</h4>
<p>They seem to lower directly to the llvm intrinsics but I admittedly haven't dug into the codegen at all</p>



<a name="212233466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233466">(Oct 04 2020 at 17:54)</a>:</h4>
<p>I believe that's the intended effect, yes.</p>



<a name="212233500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233500">(Oct 04 2020 at 17:55)</a>:</h4>
<p>If it turns out that it requires a safe-transmute operation to get between the portable SIMD types and the native ones, that'd be OK, but if there's some way to avoid that it'd be nice to avoid it. Either way, the critical thing would be that it needs to compile to zero code, and just reuse the same register.</p>



<a name="212233692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212233692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212233692">(Oct 04 2020 at 17:58)</a>:</h4>
<p>Yeah, right now it just uses transmute which should do that</p>



<a name="212234183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212234183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212234183">(Oct 04 2020 at 18:08)</a>:</h4>
<p>I think that <code>From</code> and <code>Into</code> are a poor fit for this sort of conversion.</p>



<a name="212234215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212234215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212234215">(Oct 04 2020 at 18:09)</a>:</h4>
<p>So either we just accept that it's a poor fit and put in the impls anyway and move on with life, or we just use other methods that aren't specifically <code>from</code> and <code>into</code>.</p>



<a name="212234279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212234279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212234279">(Oct 04 2020 at 18:10)</a>:</h4>
<p>But we should require an explicit conversion to re-interpret the bit layout of a register in general.</p>



<a name="212234326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212234326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212234326">(Oct 04 2020 at 18:11)</a>:</h4>
<p>I don't see any reason to avoid transmutation here, and once safe transmutation is available i don't see any reason to avoid that.</p>



<a name="212234384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212234384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212234384">(Oct 04 2020 at 18:12)</a>:</h4>
<p>Ultimately, this <em>is</em> a transmutation, and there's no way around that. The only thing to bother with is how safe and ergonomic we can make it.</p>



<a name="212234700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Casts/near/212234700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Casts.html#212234700">(Oct 04 2020 at 18:19)</a>:</h4>
<blockquote>
<p>You should be able to safe-transmute Vec&lt;u16&gt; to Vec&lt;u8&gt;, and i8 to u8 or vice versa.</p>
</blockquote>
<p>Small niggle here: you cannot. Alignment changes are illegal with <code>Vec</code> casting because of the allocator. You could do this with an array/slice though.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>